home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvidoc / dvityext.c < prev    next >
C/C++ Source or Header  |  1994-04-24  |  7KB  |  276 lines

  1. /*
  2.     External procedures for use with dvidoc.
  3.     Adapted from those used in dvip.
  4.     Written by H. Trickey, 9/3/83.
  5.     
  6.     Implement the following procedures (as declared in Pascal)
  7.  
  8.     procedure opendvifile;
  9.     function getbyte: integer;
  10.     function signedbyte: integer;
  11.     function gettwobytes: integer;
  12.     function signedpair: integer;
  13.     function getthreebytes: integer;
  14.     function signedtrio: integer;
  15.     function signedquad: integer;
  16.     function curpos(var f:bytefile):integer;
  17.     procedure setpos(var f:bytefile;n:integer);
  18.     procedure setpaths;
  19.     function testaccess(accessmode:integer; filepath:integer):boolean;
  20.  
  21.     Declaration for eofdvifile changed from int to char to match
  22.     Pascal boolean, otherwise fails on 68000's (byte ordering problem).
  23.  
  24.     Ken Yap, Feb 1986
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <strings.h>
  29. #include "texpaths.h"
  30.  
  31. static FILE *dvif;
  32.  
  33. extern char eofdvifile; /* needs to be set true when this happens */
  34. extern int curloc; /* needs to be incremented by getbyte, signedbyte */
  35.  
  36. #define namelength 100   /* should agree with dvitype.ch */
  37. extern char curname[],realnameoffile[]; /* these have size namelength */
  38.  
  39. /* open the dvifile, using the name in realnameoffile */
  40. opendvifile()
  41. {
  42.     register int i;
  43.  
  44.     for (i = namelength - 1; i >= 0; --i)
  45.         if (realnameoffile[i] == ' ')
  46.             realnameoffile[i] = '\0';
  47.         else
  48.             break;
  49.     if (!(dvif=fopen(realnameoffile,"r")))
  50.         eofdvifile = 1;
  51.     else
  52.         eofdvifile = 0;
  53. }
  54.  
  55. /* return unsigned version of next byte in dvifile */
  56. int getbyte()
  57. {
  58.     register int c;
  59.     if ((c = getc(dvif))==EOF) {
  60.         eofdvifile = 1;
  61.         return(0);
  62.         }
  63.     curloc++;
  64.     return(c);
  65. }
  66.  
  67. /* return signed version of next byte in dvifile */
  68. int signedbyte()
  69. {    register int c;
  70.     if ((c = getc(dvif))==EOF) {
  71.         eofdvifile = 1;
  72.         return(0);
  73.         }
  74.     curloc++;
  75.     if (c>127) return(c-256);
  76.     return(c);
  77. }
  78.  
  79. /* return unsigned value of next two bytes (high order first) */
  80. int gettwobytes()
  81. {
  82.     register int a,b;
  83.     a = getc(dvif);
  84.     eofdvifile = ((b = getc(dvif))==EOF);
  85.     curloc += 2;
  86.     return((a << 8) | b);
  87. }
  88.  
  89. /* return signed value of next two bytes (high order first) */
  90. int signedpair()
  91. {
  92.     register int a,b;
  93.     if ( (a = getc(dvif)) > 127) a -= 256; /* sign extend */
  94.     eofdvifile = ((b = getc(dvif))==EOF);
  95.     curloc += 2;
  96.     return((a << 8) | b);
  97. }
  98.  
  99. /* return unsigned value of next three bytes */
  100. int getthreebytes()
  101. {
  102.     register int a,b,c;
  103.     a = getc(dvif);
  104.     b = getc(dvif);
  105.     eofdvifile = ((c = getc(dvif))==EOF);
  106.     curloc +=3;
  107.     return((a << 16) | (b << 8) | c);
  108. }
  109.  
  110. /* return signed value of next three bytes */
  111. int signedtrio()
  112. {
  113.     register int a,b,c;
  114.     if ( (a = getc(dvif)) > 127) a -= 256;
  115.     b = getc(dvif);
  116.     eofdvifile = ((c = getc(dvif))==EOF);
  117.     curloc +=3;
  118.     return((a << 16) | (b << 8) | c);
  119. }
  120.  
  121. /* return value of next four bytes */
  122. int signedquad()
  123. {
  124.     register int a,b,c,d;
  125.     a = getc(dvif);
  126.     b = getc(dvif);
  127.     c = getc(dvif);
  128.     eofdvifile = ((d = getc(dvif))==EOF);
  129.     curloc += 4;
  130.     return((a << 24) | (b << 16) | (c << 8) | d);
  131. }
  132.  
  133. /* seek to byte n of file f: actually, assume f is for dvifile */
  134. setpos(f,n)
  135.     int f; /* not really, but we ignore it */
  136.     int n;
  137. {
  138.     if (n>=0) {
  139.         fseek(dvif,(long) n,0); 
  140.         eofdvifile = 0;
  141.         }
  142.     else {
  143.         fseek(dvif, (long) n, 2);
  144.         eofdvifile = 1;
  145.         }
  146. }
  147.  
  148. /* return current byte offset in file f (again, assume dvifile) */
  149. int curpos(f)
  150.     int f;
  151. {
  152.     return((int) ftell(dvif));
  153. }
  154.  
  155. char *fontpath;
  156.  
  157. char *getenv();
  158.  
  159. /*
  160.  * setpaths is called to set up the pointer fontpath
  161.  * as follows:  if the user's environment has a value for TEXFONTS
  162.  * then use it;  otherwise, use defaultfontpath.
  163.  */
  164. setpaths()
  165. {
  166.     register char *envpath;
  167.     
  168.     if ((envpath = getenv("TEXFONTS")) != NULL)
  169.         fontpath = envpath;
  170.     else
  171.         fontpath = defaultfontpath;
  172. }
  173.  
  174. /*
  175.  *    testaccess(amode,filepath)
  176.  *
  177.  *  Test whether or not the file whose name is in the global curname
  178.  *  can be opened for reading (if mode=READACCESS)
  179.  *  or writing (if mode=WRITEACCESS).
  180.  *
  181.  *  The filepath argument is one of the ...FILEPATH constants defined below.
  182.  *  If the filename given in curname does not begin with '/', we try 
  183.  *  prepending all the ':'-separated areanames in the appropriate path to the
  184.  *  filename until access can be made, if it ever can.
  185.  *
  186.  *  The realnameoffile global array will contain the name that yielded an
  187.  *  access success.
  188.  */
  189.  
  190. #define READACCESS 4
  191. #define WRITEACCESS 2
  192.  
  193. #define NOFILEPATH 0
  194. #define FONTFILEPATH 3
  195.  
  196. #if !defined(AIX) && !defined(__osf__)
  197. typedef enum {FALSE, TRUE} bool;
  198. #else
  199. typedef int bool;
  200. #endif
  201.  
  202. bool
  203. testaccess(amode,filepath)
  204.     int amode,filepath;
  205. {
  206.     register bool ok;
  207.     register char *p;
  208.     char *curpathplace;
  209.     int f;
  210.     
  211.     switch(filepath) {
  212.     case NOFILEPATH: curpathplace = NULL; break;
  213.     case FONTFILEPATH: curpathplace = fontpath; break;
  214.     }
  215.     if (curname[0]=='/')    /* file name has absolute path */
  216.     curpathplace = NULL;
  217.     do {
  218.     packrealnameoffile(&curpathplace);
  219.     if (amode==READACCESS)
  220.         /* use system call "access" to see if we could read it */
  221.         if (access(realnameoffile,READACCESS)==0) ok = TRUE;
  222.         else ok = FALSE;
  223.     else {
  224.         /* WRITEACCESS: use creat to see if we could create it, but close
  225.         the file again if we're OK, to let pc open it for real */
  226.         f = creat(realnameoffile,0666);
  227.         if (f>=0) ok = TRUE;
  228.         else ok = FALSE;
  229.         if (ok)
  230.         close(f);
  231.         }
  232.     } while (!ok && curpathplace != NULL);
  233.     if (ok) {  /* pad realnameoffile with blanks, as Pascal wants */
  234.     for (p = realnameoffile; *p != '\0'; p++)
  235.         /* nothing: find end of string */ ;
  236.     while (p < &(realnameoffile[namelength]))
  237.         *p++ = ' ';
  238.     }
  239.     return (ok);
  240. }
  241.  
  242. /*
  243.  * packrealnameoffile(cpp) makes realnameoffile contain the directory at *cpp,
  244.  * followed by '/', followed by the characters in curname up until the
  245.  * first blank there, and finally a '\0'.  The cpp pointer is left pointing
  246.  * at the next directory in the path.
  247.  * But: if *cpp == NULL, then we are supposed to use curname as is.
  248.  */
  249. packrealnameoffile(cpp)
  250.     char **cpp;
  251. {
  252.     register char *p,*realname;
  253.     
  254.     realname = realnameoffile;
  255.     if ((p = *cpp)!=NULL) {
  256.     while ((*p != ':') && (*p != '\0')) {
  257.         *realname++ = *p++;
  258.         if (realname == &(realnameoffile[namelength-1]))
  259.         break;
  260.         }
  261.     if (*p == '\0') *cpp = NULL; /* at end of path now */
  262.     else *cpp = p+1; /* else get past ':' */
  263.     *realname++ = '/';  /* separate the area from the name to follow */
  264.     }
  265.     /* now append curname to realname... */
  266.     p = curname;
  267.     while ((*p != ' ') && (*p != '\0')) {
  268.     if (realname >= &(realnameoffile[namelength-1])) {
  269.         fprintf(stderr,"! Full file name is too long\n");
  270.         break;
  271.         }
  272.     *realname++ = *p++;
  273.     }
  274.     *realname = '\0';
  275. }
  276.